package cn.iocoder.yudao.module.contract.service.basicData;

import cn.iocoder.yudao.module.contract.dal.dataobject.client.ClientDO;
import cn.iocoder.yudao.module.contract.dal.mysql.client.ClientMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.contract.controller.admin.basicData.vo.*;
import cn.iocoder.yudao.module.contract.dal.dataobject.basicData.BasicSupplierDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.contract.dal.mysql.basicData.BasicSupplierMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.contract.enums.ErrorCodeConstants.*;

/**
 * 供应商 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class BasicSupplierServiceImpl extends ServiceImpl<BasicSupplierMapper, BasicSupplierDO> implements BasicSupplierService {

    @Resource
    private BasicSupplierMapper basicSupplierMapper;

    @Override
    public Long createBasicSupplier(BasicSupplierSaveReqVO createReqVO) {
        // 插入
        BasicSupplierDO basicSupplier = BeanUtils.toBean(createReqVO, BasicSupplierDO.class);
        basicSupplierMapper.insert(basicSupplier);
        // 返回
        return basicSupplier.getId();
    }

    @Override
    public void updateBasicSupplier(BasicSupplierSaveReqVO updateReqVO) {
        // 校验存在
        validateBasicSupplierExists(updateReqVO.getId());
        // 更新
        BasicSupplierDO updateObj = BeanUtils.toBean(updateReqVO, BasicSupplierDO.class);
        basicSupplierMapper.updateById(updateObj);
    }

    @Override
    public void deleteBasicSupplier(Long id) {
        // 校验存在
        validateBasicSupplierExists(id);
        // 删除
        basicSupplierMapper.deleteById(id);
    }

    private void validateBasicSupplierExists(Long id) {
        if (basicSupplierMapper.selectById(id) == null) {
            throw exception(BASIC_SUPPLIER_NOT_EXISTS);
        }
    }

    @Override
    public BasicSupplierDO getBasicSupplier(Long id) {
        return basicSupplierMapper.selectById(id);
    }

    @Override
    public PageResult<BasicSupplierDO> getBasicSupplierPage(BasicSupplierPageReqVO pageReqVO) {
        return basicSupplierMapper.selectPage(pageReqVO);
    }

}